<template>
    <div class="count">
        <!-- <div>
            <h2>当前求和为：{{ countStore.sum }}</h2>
            <h2>当前学校为：{{ countStore.school }}</h2>
            <h2>当前学科为：{{ countStore.subject }}</h2>
        </div> -->
        <div>
            <h2>当前求和为：{{ sum }}</h2>
            <h2>当前学校为：{{ school }}</h2>
            <h2>当前学科为：{{ subject }}</h2>
            <h2>当前求和的10倍为：{{ bigSum }}</h2>
            <h2>学校变大为：{{ bigSchool }}</h2>
        </div>
        <div class="count-controls">
            <select class="count-select" v-model.number="n">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
            </select>
            <div class="count-buttons">
                <button class="btn" @click="increment">+</button>
                <button class="btn" @click="decrement">-</button>
                <button class="btn reset" @click="reset">重置</button>
                <button class="btn reset" @click="allChange">一键修改</button>
            </div>

        </div>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useCountStore } from '../store/count';
import { storeToRefs } from 'pinia';

const countStore = useCountStore()
// storeToRefs只会关注state中的数据，不会关注actions中的数据，解构出来的数据是响应式的
const { sum, school, subject, bigSum, bigSchool } = storeToRefs(countStore)

// 以下两种写法都可以拿到state中的数据
console.log('sum1', countStore.sum);
console.log('sum2', countStore.$state.sum);
console.log('sum3', sum.value);

// 会直接帮忙解包
let obj = reactive({
    a: 1,
    b: 2,
    c: ref(3)
})
console.log('obj---', obj.a, obj.b, obj.c);//c:不需要.value
let x = ref(1)
console.log('x---', x.value);//x:需要.value


const n = ref(1);
// 第一种修改方式——直接修改
const increment = () => {
    countStore.sum += n.value;
}

// 第二种修改方式——通过$patch修改:可以一次性修改多个数据
function allChange() {
    countStore.$patch({
        school: '北京',
        subject: 'vue',
        sum: 100
    })
}

// 第三种修改方式——通过actions修改
const decrement = () => {
    countStore.decrement(n.value);
}

const reset = () => {
    sum.value = 0;
}
</script>

<style scoped>
.count {
    width: 400px;
    padding: 20px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.count h2 {
    color: #333;
    margin-bottom: 20px;
    text-align: center;
}

.count-controls {
    display: flex;
    flex-direction: column;
    gap: 15px;
    align-items: center;
}

.count-select {
    padding: 8px 16px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 16px;
    width: 120px;
    cursor: pointer;
}

.count-select:focus {
    outline: none;
    border-color: #409eff;
}

.count-buttons {
    display: flex;
    gap: 10px;
}

.btn {
    padding: 8px 20px;
    border: none;
    border-radius: 4px;
    background-color: #409eff;
    color: white;
    cursor: pointer;
    transition: background-color 0.3s;
}

.btn:hover {
    background-color: #66b1ff;
}

.btn.reset {
    background-color: #f56c6c;
}

.btn.reset:hover {
    background-color: #f78989;
}
</style>